package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.RightsMapper;
import com.jt.pojo.Rights;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RightsServiceImpl implements RightsService{

    @Autowired
    private RightsMapper rightsMapper;


    @Override
    public List<Rights> getAll() {
        //查询全部不需要条件
        return rightsMapper.selectList(null);
    }

    /**
     * 需求: 查询2级菜单信息.
     * 步骤:
     *      1.查询一级菜单信息 parent_id = 0
     *      2.查询二级菜单信息 parent_id = 一级菜单ID
     *      3.将二级菜单封装到一级菜单中.
     *  查询方式:
     *          1.利用java循环实现2级封装.
     *          2.利用Mybatis的封装的方式实现查询 resultMap 自己练习
     * @return
     */
    @Override
    public List<Rights> getRightsList() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id",0);
        List<Rights> oneList = rightsMapper.selectList(queryWrapper);
        //分析: 1级和2级有各自的对应的关系. 拿到当前的一级查询自己的二级菜单.
        for (Rights oneRights : oneList){
            //应该先清空原有条件
            queryWrapper.clear();
            //根据一级id查询二级信息.
            queryWrapper.eq("parent_id",oneRights.getId());
            List<Rights> twoList = rightsMapper.selectList(queryWrapper);
            //实现父子关系的封装
            oneRights.setChildren(twoList);
        }
        return oneList;
    }
}